
package org.prx.prp;

import org.prx.prp.IBackgroundDataService;
import org.prx.prp.controller.SyncManager;
import org.prx.prp.utility.DatabaseAdapter;
import org.prx.prp.utility.DatabaseHelper;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class BackgroundDataService extends Service {
	
	private boolean mIsSyncing = false;
	private boolean mIsPaused = false;
	private boolean mSyncIsDone = false;
	
	
	public IBinder onBind(Intent intent){
		return mBinder;
	}
	
	private final IBackgroundDataService.Stub mBinder = new IBackgroundDataService.Stub() {
		
		
		
		private Thread mSyncThread = new Thread(new Runnable(){
			@Override
			public void run(){
				//DatabaseHelper mDatabaseHelper = new DatabaseHelper(BackgroundDataService.this, "org.prx");
		        DatabaseAdapter.initDatabaseAdapter(BackgroundDataService.this);
		        SyncManager mSyncManager = new SyncManager();
				mSyncManager.syncModels();
				mSyncIsDone = true;
			}
		});
		
		@Override
		public void startSync() {
			mIsPaused = false;
			mIsSyncing = true;
			mSyncThread.start();
		}
		
		@Override
		public void pauseSync() {
			mIsPaused = true;
			Log.d("PRPAND", "Pausing Sync!");
			mSyncThread.suspend();
		}
		
		@Override
		public void stopSync() {
			mIsPaused = false;
			mIsSyncing = false;
			mSyncThread.stop();
			Log.d("PRPAND", "Stopping Sync!");
		}
		
		@Override
		public boolean isPaused() {
			return mIsPaused;
		}
		
		@Override
		public boolean isSyncing() {
			return mIsSyncing;
		}
	};
	
}